 /*
 * @description: 精英pk赛/精英排行榜
 * @Author: along
 * @Date: 2021-08-09
 * @Last Modified by: along
 * @Last Modified time: 2021-08-09
 */
<template>
    <div class="container zsdxElitePkRank">
        <!-- header -->
        <div class="header-nav">
            <div class="header-left">
                <div class="header-new-box">
                    <div class="header-new-box-title">
                        PK赛类型：
                    </div>
                    <el-select
                        v-model="filterData.pk_type"
                        size="small"
                        style="width: 152px"
                        @change="handleTypeChange"
                    >
                        <el-option
                            v-for="item in typeList"
                            :key="item.value"
                            :label="item.label"
                            :value="item.value"
                        />
                    </el-select>
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">活动期数:</p>
                    <el-select
                        v-model="formData.active_number"
                        clearable
                        filterable
                        style="width: 152px"
                        placeholder="请选择"
                        size="small"
                        @change="getTableData(1)"
                    >
                        <el-option
                            v-for="item in activeList"
                            :key="item.index"
                            :label="item.title"
                            :value="item.index"
                        />
                    </el-select>
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">用户信息:</p>
                    <el-input
                        v-model="formData.keyword"
                        placeholder="请输入用户编号/手机号"
                        size="small"
                        clearable
                        suffix-icon="el-icon-search"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">所属学校:</p>
                    <el-cascader
                        v-model="filterData.school_name"
                        placeholder="请选择学校"
                        :options="school_tree"
                        filterable
                        clearable
                        :show-all-levels="false"
                        size="small"
                        change-on-select
                        expand-trigger="click"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                        :props="{
                            label: 'name',
                            value: 'name',
                            children: 'children',
                        }"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">精英等级:</p>
                    <el-select
                        v-model="formData.agent_level"
                        clearable
                        filterable
                        style="width: 152px"
                        placeholder="请选择"
                        size="small"
                        @change="getTableData(1)"
                    >
                        <el-option
                            label="全部"
                            value="99"
                        />
                        <el-option
                            v-for="item in 6"
                            :key="item"
                            :label="`E${item - 1}`"
                            :value="item - 1"
                        />
                    </el-select>
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">邀请码:</p>
                    <el-input
                        v-model="formData.code"
                        placeholder="请输入邀请码"
                        size="small"
                        clearable
                        suffix-icon="el-icon-search"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                    />
                </div>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;margin-top: 9px"
            class="data-table"
            ref="zsdxElitePkRank"
            @sort-change="handleSortChanged"
        >
            <el-table-column
                type="index"
                width="80"
                label="序号"
                align="center"
            />
            <el-table-column
                prop="index"
                label="活动期数"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            >
                <template slot-scope="scope">
                    第{{ scope.row.index ? scope.row.index : '0' }}期
                </template>
            </el-table-column>
            <el-table-column
                label="PK赛类型"
                show-overflow-tooltip
                width="100"
                :formatter="emptyFormatter"
            >
                <template slot-scope="scope">
                    <div v-if="scope.row.pk_type === 1">个人PK赛</div>
                    <div v-else-if="scope.row.pk_type === 2">学校PK赛</div>
                    <div v-else>--</div>
                </template>
            </el-table-column>
            <el-table-column
                prop="true_name"
                label="用户信息"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
                :width="180"
            >
                <template slot-scope="scope">
                    <div class="__flex_wrap">
                        <p class="__flex_wrap_title">{{ scope.row.true_name }}</p>
                        <p>手机号:{{ scope.row.phone }}</p>
                        <p>精英等级:E{{ scope.row.agent_level }}</p>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                prop="school_name"
                label="学校名称"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
                :width="150"
            />
            <el-table-column
                label="活动资格"
                show-overflow-tooltip
                align="left"
                :formatter="emptyFormatter"
                :width="120"
            >
                <template slot-scope="scope">
                    <div class="status-wrapper">
                        <div
                            :class="['status-dot',scope.row.cancel_status === 1 ? 'success' : 'danger']"
                        />
                        <div v-if="scope.row.cancel_status === 1">未取消</div>
                        <div
                            v-if="scope.row.cancel_status === -1"
                            style="color: #F95B56">取消资格</div>
                        <div
                            v-if="scope.row.cancel_status === -2"
                            style="color: #F95B56">淘汰</div>
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                prop="invite_code"
                label="邀请码"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
            >
                <template slot-scope="scope">
                    <div class="__flex_2">
                        <p
                            :style="{color: '#409eff', cursor: 'pointer'}"
                            @click="handleCopy(scope.row.invite_code)"
                        >{{ scope.row.invite_code }}</p>
                        <!-- <i
                            class="iconfont el-icon-hand-fuzhi"
                        /> -->
                    </div>
                </template>
            </el-table-column>
            <el-table-column
                prop="self_order_count"
                label="自购订单数"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
                sortable="custom"
                :width="130"
            >
                <template slot-scope="scope">
                    <p
                        class="cursor-font"
                        @click="handleNextDetail(scope.row, 'eliteSelfOrder')"
                    >{{ scope.row.self_order_count ? scope.row.self_order_count : 0 }}</p>
                </template>
            </el-table-column>
            <el-table-column
                prop="friends_order_count"
                label="好友订单数"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
                sortable="custom"
                :width="150"
            >
                <template slot-scope="scope">
                    <p
                        class="cursor-font"
                        @click="handleNextDetail(scope.row, 'eliteAgentOrder', 'directly')"
                    >{{ scope.row.friends_order_count ? scope.row.friends_order_count : 0 }}</p>
                </template>
            </el-table-column>
            <!--<el-table-column
                prop="grand_fans_order_count"
                label="间接精英订单数"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
                sortable="custom"
                :width="150"
            >
                <template slot-scope="scope">
                    <p
                        class="cursor-font"
                        @click="handleNextDetail(scope.row, 'eliteAgentOrder', 'indirect')"
                    >{{ scope.row.grand_fans_order_count ? scope.row.grand_fans_order_count : 0 }}</p>
                </template>
            </el-table-column>-->
            <el-table-column
                prop="team_order_count"
                label="团队订单数"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
                sortable="custom"
                :width="140"
            >
                <template slot-scope="scope">
                    {{ scope.row.team_order_count ? scope.row.team_order_count : 0 }}
                </template>
            </el-table-column>
            <el-table-column
                prop="team_order_refund_count"
                label="团队退款订单数"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
                sortable="custom"
                :width="150"
            >
                <template slot-scope="scope">
                    {{ scope.row.team_order_refund_count ? scope.row.team_order_refund_count : 0 }}
                </template>
            </el-table-column>
            <el-table-column
                prop="team_order_money"
                label="团队实付金额"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
                sortable="custom"
                :width="140"
            >
                <template slot-scope="scope">
                    {{ scope.row.team_order_money ? scope.row.team_order_money : '0.00' }}
                </template>
            </el-table-column>
            <el-table-column
                prop="team_real_order_count"
                label="最终有效订单数"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="left"
                sortable="custom"
                :width="150"
            >
                <template slot-scope="scope">
                    {{ scope.row.team_real_order_count ? scope.row.team_real_order_count : 0 }}
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- /分页 -->
        <div class="pagination-wrapper clearfix">
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div>
    </div>
</template>

<script>
import { back } from '@/mixin/back';
export default {
    mixins: [back],
    components: { },
    data () {
        return {
            formData: {
                keyword: '',
                school_name: '',
                agent_level: '99',
                code: '',
                active_number: ''
            },
            school_tree: [],
            activeList: [],
            filterData: {
                sort_field: '',
                sort_type: 0,
                pk_type: 2
            },
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            tableData: [],
            authResult: {},
            activity_info: {},
            typeList: [
                {label: '全部', value: 99},
                {label: '个人PK赛', value: 1},
                {label: '学校PK赛', value: 2},
            ],
        };
    },
    filters: {},
    computed: {},
    watch: {},
    mounted () {
        this.$store.commit('SET_HEADER', [{ title: "APP后台"},{title: '校园精英pk赛'},{title: '统计列表'},{title: '精英排行榜'}]);
        this.authResult = this.$route.meta.authResult;
        this.getSchoolTree();
        this.getActiveNumber(this.filterData.pk_type);
    },
    methods: {
        /**
         * @description 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/task_stuagent_extra_pk_activity%5Cactivity_statistic%5Cget_stuagent_ranking_list', {
                page_id: page || this.page.now_page,
                sort_field: this.filterData.sort_field,
                sort_type: this.filterData.sort_type,
                keyword: this.formData.keyword,
                invite_code: this.formData.code,
                school_name: this.formData.school_name,
                agent_level: this.formData.agent_level,
                index: this.formData.active_number,
                pk_type: this.filterData.pk_type
            }, resp => {
                if (resp.code == 1) {
                    if(!!resp.data.page && resp.data.page.now_page === 1) {
                        this.activity_info = !!resp.data.activity_info ? resp.data.activity_info : {
                            title: '',
                            index: '',
                            activity_start_time: '',
                            activity_end_time: '',
                            rank_end_time: ''
                        };
                        this.$emit('emit', {
                            type: 'elite-rank',
                            data: this.activity_info
                        });
                    }
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * @description 获取省/市/学校三级联动
         */
        getSchoolTree () {
            this.$post('/student/local_info%5Cschool_tree',{},resp => {
                if (resp.code === 1) {
                    this.school_tree = resp.data.tree;
                }
            });
        },

        /**
         * @description 获取期数
         */
        getActiveNumber (pk_type = 99) {
            this.$post('/student/task_stuagent_extra_pk_activity%5Cactivity_config%5Cget_list_for_select',{pk_type},resp => {
                if (resp.code === 1) {
                    const activeList = resp.data.list.reduce((cur,next) => {
                        cur.push({
                            index: next.index,
                            title: next.index
                        });
                        return cur;
                    }, []);

                    this.activeList = [{index: -1, title: '全部'}].concat(activeList);
                    this.$set(this.formData, 'active_number', !!this.activeList ? (!!this.activeList.length ? this.activeList[0]['index'] : -1) : -1);
                    this.getTableData();
                } else {
                    this.getTableData();
                }
            });
        },

        /**
         * @description 页面跳转
         */
        handleNextDetail (row, name, type = '') {
            this.$router.push({
                name: name,
                query: {
                    no: row.apply_no,
                    type: type
                }
            });
        },

        /**
         * @description 复制邀请码
         */
        handleCopy (value) {
            let str = value;
            let oInput = document.createElement("input");

            oInput.value = str;
            document.body.appendChild(oInput);
            oInput.select();
            document.execCommand("Copy");
            oInput.style.display = "none";
            this.$notify({
                title: '提示',
                message: '邀请码已复制',
                type: 'success',
                duration: 2000
            });
        },

        /**
         * @description 数据排序
         */
        handleSortChanged(data) {
            let sortTypeMap = {
                ascending: 1,
                descending: 0
            };

            let sortFieldMap = {
                self_order_count: 'self_order_count',
                friends_order_count: 'friends_order_count',
                team_order_count: 'team_order_count',
                team_order_refund_count: 'team_order_refund_count',
                team_order_money: 'team_order_money',
                team_real_order_count: 'team_real_order_count',
                grand_fans_order_count: 'grand_fans_order_count'
            };

            if (data.order) {
                this.filterData.sort_field = sortFieldMap[data.prop];
                this.filterData.sort_type = sortTypeMap[data.order];
            } else {
                this.filterData.sort_field = '';
                this.filterData.sort_type = '';
            }

            this.getTableData();
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 类型变化
         * @param e
         */
        handleTypeChange(e) {
            this.getActiveNumber(e);
            this.formData.active_number = -1;
            this.getTableData(1);
        }
    }
};
</script>

<style lang="less" scoped>
    .zsdxElitePkRank {
        background-color: #fff;
        padding: 16px 32px 5px 32px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        // height: 100%;
        box-sizing: border-box;
        .header-nav {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            .header-left, .header-right {
                display: flex;
                align-items: center;
                flex-wrap: wrap;
            }
            .header-new-box {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 10px;
                white-space: nowrap;
                &-title {
                    margin-right: 2px;
                    white-space: nowrap;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding-top: 10px;
            padding-bottom: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    .status-wrapper {
        display: flex;
        align-items: center;
        justify-content: flex-start;
        width: 120px;
        .status-dot {
            width: 6px;
            height: 6px;
            border-radius: 50%;
            margin-right: 4px;
        }
        .success {
            background: #4CD964;
        }
        .danger {
            background: #FF3030;
        }
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .el-button-left {
        margin-left: 40px;
    }
    .el-button-right {
        margin-right: 10px;
    }
    .__flex_wrap {
        display: flex;
        flex-direction: column;
        justify-content: flex-start;
        align-items: flex-start;
        .__flex_wrap_title {
            color: rgba(51, 51, 51, 1);
            font-size: 14px;
            font-weight: 500;
        }
    }
    .__flex_2 {
        display: flex;
        align-items: center;
        .el-icon-hand-fuzhi {
            color: #20a0ff;
            cursor: pointer;
            font-size: 16px;
            margin-left: 4px;
        }
    }
    // 表格
    .data-table {
        ::-webkit-scrollbar {
            width: 6px;
            height: 6px;
        }
    }
</style>
